//
//  DrawTraceLayer.cpp
//  firebkball
//
//  Created by flydy on 2018/5/14.
//
//

#include "DrawTraceLayer.h"
USING_NS_CC;

bool DrawTraceLayer::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    drawNode = DrawNode::create();
    this->addChild(drawNode);
    return true;
}

void DrawTraceLayer::onEnter()
{
    Layer::onEnter();
}

void DrawTraceLayer::clearTrace()
{
    drawNode->clear();
}

void DrawTraceLayer::refreshTraceView(cocos2d::Vec2 sp, cocos2d::Vec2 ep, cocos2d::Vec2 v, cocos2d::Vec2 g, float factor)
{
    drawNode->clear();
    drawNode->drawSegment(sp, ep, 0.1, Color4F(0, 1.0, 0, 1));
	Vec2 fp;
	fp.x = ep.x + v.x;
	fp.y = ep.y + v.y;

	drawNode->drawSegment(ep, Vec2(fp.x, ep.y),0.5f, Color4F(0, 1.0, 0, 1));
	drawNode->drawSegment(ep, Vec2(ep.x, fp.y),0.5f, Color4F(0, 1.0, 0, 1));
    
	//CCLOG("v=%f,%f",v.x, v.y);
	float dt = 0;
	for (int i = 1; i <= times; i++) {
		dt += dtime;
		float dx = VErrorRatio*factor * v.x * dt;
		float dy = factor * v.y*dt - (-GErrorRatio*g.y*dt*dt) / 2.0f;
		//(const Vec2 &pos, float radius, const Color4F &color);
		drawNode->drawDot(Vec2(sp.x+dx, sp.y+dy), 2.0f, Color4F(0, 1.0f, 0.5f, 0.6f));
	}

}
